package com.biz.primus.app.erpbridge.service;

import com.biz.primus.app.erpbridge.feign.StockFeignClient;
import com.biz.primus.base.enums.commodity.OssType;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.model.stock.vo.AllStockItemVo;
import com.biz.primus.model.stock.vo.IncrDepotStockReqVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * StockBridgeService
 *
 * @author JKLiues
 * @date 2017年11月17日
 */
@Service
@Slf4j
public class StockBridgeService extends OssFullDataHandler {

    @Autowired
    private StockFeignClient stockFeignClient;


    @Override
    public OssType[] getOssType() {
        return new OssType[]{OssType.STOCK};
    }

    @Override
    protected void preHandle(OssType type) {
        log.info("全量库存同步----------------");
    }

    @Override
    protected int handleData(List<String> jsons) {
        if (CollectionUtils.isEmpty(jsons)) {
            log.warn("全量库存处理接受到空数据集合");
            return 0;
        }
        List<AllStockItemVo> collect = jsons.stream()
                .map(json -> JsonUtils.json2Obj(json, AllStockItemVo.class))
                .collect(Collectors.toList());
        stockFeignClient.saveDepotStocks(collect);
        return collect.size();
    }

    @Override
    protected void afterHandle(OssType type) {
        stockFeignClient.afterHandleStockAll(type.name());
    }


    /**
     * 增量库存
     *
     * @param reqVo 请求vo
     */
    public void stockIncrement(IncrDepotStockReqVo reqVo){
        stockFeignClient.updateStockOnly(reqVo);
    }
}